package com.lixw.saa.controller;

import jakarta.annotation.Resource;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.messages.Message;
import org.springframework.ai.chat.messages.SystemMessage;
import org.springframework.ai.chat.messages.UserMessage;
import org.springframework.ai.chat.model.ChatModel;
import org.springframework.ai.chat.prompt.Prompt;
import org.springframework.ai.chat.prompt.PromptTemplate;
import org.springframework.ai.chat.prompt.SystemPromptTemplate;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;

import java.util.List;
import java.util.Map;

/**
 * @ClassName: PromptTemplateController
 * @Description:
 * @Author: xuweiLi
 * @Create: 2025/10/3 12:07
 **/
@RestController
public class PromptTemplateController {
    @Resource(name = "deepseekChatModel")
    private ChatModel deepseekChatModel;
    @Resource(name = "qwenChatModel")
    private ChatModel qwenChatModel;

    @Resource(name = "deepseekChatClient")
    private ChatClient deepseekChatClient;
    @Resource(name = "qwenChatClient")
    private ChatClient qwenChatClient;

    @Value("classpath:/promptTemplate.txt")
    private org.springframework.core.io.Resource templateResource;

    @GetMapping("/prompttemplate/chat")
    public Flux<String> chat(
            @RequestParam("topic") String topic,
            @RequestParam("output_format") String output_format,
            @RequestParam("wordCount") String wordCount) {
        PromptTemplate promptTemplate = new PromptTemplate(
                """
                            讲一个关于{topic}的故事, 并且{output_format}格式输出,
                            字数在{wordCount}左右")
                        """
        );
        Prompt prompt = promptTemplate.create(
                Map.of("topic", topic, "output_format", output_format, "wordCount", wordCount)
        );
        return deepseekChatClient.prompt(prompt).stream().content();
    }

    @GetMapping("/prompttemplate/chat2")
    public Flux<String> chat(
            @RequestParam("topic") String topic,
            @RequestParam("output_format") String output_format) {
        PromptTemplate promptTemplate = new PromptTemplate(templateResource);
        Prompt prompt = promptTemplate.create(
                Map.of("topic", topic, "output_format", output_format)
        );
        return deepseekChatClient.prompt(prompt).stream().content();
    }

    @GetMapping("/prompttemplate/chat3")
    public Flux<String> chat3( @RequestParam("sysTopic") String sysTopic,
                               @RequestParam("userTopic") String userTopic){
        //1. 系统提示模板
        SystemPromptTemplate systemPromptTemplate = new SystemPromptTemplate("你是{systemTopic}助手，只回答{systemTopic}其它无可奉告，以HTML格式的结果。");
        Message systemMessage = systemPromptTemplate.createMessage(Map.of("systemTopic", sysTopic));
        //2. 用户提示词模板
        PromptTemplate userPromptTemplate = new PromptTemplate("解释一下{userTopic}");
        Message userMessage = userPromptTemplate.createMessage(Map.of("userTopic", userTopic));
        //3. 组合两个消息为提示词
        Prompt prompt = new Prompt(List.of(systemMessage, userMessage));
        //4. 调用LLM
        return deepseekChatClient.prompt(prompt).stream().content();
    }

    @GetMapping("/prompttemplate/chat4")
    public String chat4(@RequestParam("question") String question){
        //1. 系统消息
        SystemMessage systemMessage = new SystemMessage("你是一个Java编程助手,拒绝回答非技术性的问题");
        //2. 用户消息
        UserMessage userMessage = new UserMessage(question);
        //3. 系统消息 + 用户消息 = 完整的提示词
        Prompt prompt = Prompt.builder().messages(List.of(systemMessage, userMessage)).build();
        //4. 调用LLM
        String result = deepseekChatModel.call(prompt).getResult().getOutput().getText();
        return result;
    }

    @GetMapping("/prompttemplate/chat5")
    public Flux<String> chat5(@RequestParam("question") String question){
        return deepseekChatClient.prompt()
                .system("你是一个Java编程助手，拒绝回答非技术问题。")
                .user(question)
                .stream()
                .content();
    }
}